﻿using System;
using System.Globalization;
using System.Web.UI.WebControls;

namespace MSTR.IS.Util
{
    public static class Parser
    {
        /// <summary>
        /// Truncate string if it exceeds a specified maximum length.
        /// </summary>
        /// <param name="s">String to truncate.</param>
        /// <param name="maxLength">Maximum length that is allowed.</param>
        public static string Truncate(this string s, int maxLength)
        {
            if (maxLength <= 0)
                throw new ArgumentOutOfRangeException("maxLength", "maxLength must be greater than zero.");

            if (maxLength >= s.Length)
                return s;

            return s.Substring(0, maxLength);
        }

        public static int? ToInt(this string s)
        {
            int value;
            if (int.TryParse(s, out value))
                return value;

            return null;
        }

        public static short? ToShort(this string s)
        {
            short value;
            if (short.TryParse(s, out value))
                return value;

            return null;
        }

        public static long? ToLong(this string s)
        {
            long value;
            if (long.TryParse(s, out value))
                return value;

            return null;
        }

        public static double? ToDouble(this string s)
        {
            double value;
            if (double.TryParse(s, out value))
                return value;

            return null;
        }

        public static bool? ToBool(this string s)
        {
            bool value;
            if (bool.TryParse(s, out value))
                return value;

            return null;
        }

        public static DateTime? ToDate(this string s)
        {
            DateTime dt;
            if (DateTime.TryParse(s, out dt))
                return dt;

            return null;
        }

        public static DateTime? ToDateExact(this string s, string format, string cultureName)
        {
            if (null == s || null == format || null == cultureName)
                return null;

            try
            {
                var culture = CultureInfo.GetCultureInfo(cultureName);
                DateTime dt;
                if (DateTime.TryParseExact(s, format, culture.DateTimeFormat, DateTimeStyles.AllowWhiteSpaces, out dt))
                    return dt;
            }
            catch
            { }

            return null;
        }

        public static int? ParseInt(this ListControl lc)
        {
            if (null == lc || null == lc.SelectedItem)
                return null;

            return lc.SelectedValue.ToInt();
        }

        public static long? ParseLong(this ListControl lc)
        {
            if (null == lc || null == lc.SelectedItem)
                return null;

            return lc.SelectedValue.ToLong();
        }

        public static bool? ParseBool(this ListControl lc)
        {
            if (null == lc || null == lc.SelectedItem)
                return null;

            return lc.SelectedValue.ToBool();
        }
    }
}
